package com.shen.accountbook.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.shen.accountbook.bean.temp.SecondSortBean;

import com.shen.accountbook.bean.temp.ThirdSortBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "THIRD_SORT_BEAN".
*/
public class ThirdSortBeanDao extends AbstractDao<ThirdSortBean, Long> {

    public static final String TABLENAME = "THIRD_SORT_BEAN";

    /**
     * Properties of entity ThirdSortBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property First_sort_id = new Property(1, Long.class, "first_sort_id", false, "FIRST_SORT_ID");
        public final static Property Second_sort_id = new Property(2, Long.class, "second_sort_id", false, "SECOND_SORT_ID");
        public final static Property Third_sort_id = new Property(3, Long.class, "third_sort_id", false, "THIRD_SORT_ID");
        public final static Property Third_sort_name = new Property(4, String.class, "third_sort_name", false, "THIRD_SORT_NAME");
        public final static Property Third_sort_img = new Property(5, String.class, "third_sort_img", false, "THIRD_SORT_IMG");
        public final static Property Frequency = new Property(6, Long.class, "frequency", false, "FREQUENCY");
    }

    private DaoSession daoSession;


    public ThirdSortBeanDao(DaoConfig config) {
        super(config);
    }
    
    public ThirdSortBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"THIRD_SORT_BEAN\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"FIRST_SORT_ID\" INTEGER NOT NULL ," + // 1: first_sort_id
                "\"SECOND_SORT_ID\" INTEGER NOT NULL ," + // 2: second_sort_id
                "\"THIRD_SORT_ID\" INTEGER UNIQUE ," + // 3: third_sort_id
                "\"THIRD_SORT_NAME\" TEXT," + // 4: third_sort_name
                "\"THIRD_SORT_IMG\" TEXT," + // 5: third_sort_img
                "\"FREQUENCY\" INTEGER);"); // 6: frequency
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"THIRD_SORT_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ThirdSortBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getFirst_sort_id());
        stmt.bindLong(3, entity.getSecond_sort_id());
 
        Long third_sort_id = entity.getThird_sort_id();
        if (third_sort_id != null) {
            stmt.bindLong(4, third_sort_id);
        }
 
        String third_sort_name = entity.getThird_sort_name();
        if (third_sort_name != null) {
            stmt.bindString(5, third_sort_name);
        }
 
        String third_sort_img = entity.getThird_sort_img();
        if (third_sort_img != null) {
            stmt.bindString(6, third_sort_img);
        }
 
        Long frequency = entity.getFrequency();
        if (frequency != null) {
            stmt.bindLong(7, frequency);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ThirdSortBean entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getFirst_sort_id());
        stmt.bindLong(3, entity.getSecond_sort_id());
 
        Long third_sort_id = entity.getThird_sort_id();
        if (third_sort_id != null) {
            stmt.bindLong(4, third_sort_id);
        }
 
        String third_sort_name = entity.getThird_sort_name();
        if (third_sort_name != null) {
            stmt.bindString(5, third_sort_name);
        }
 
        String third_sort_img = entity.getThird_sort_img();
        if (third_sort_img != null) {
            stmt.bindString(6, third_sort_img);
        }
 
        Long frequency = entity.getFrequency();
        if (frequency != null) {
            stmt.bindLong(7, frequency);
        }
    }

    @Override
    protected final void attachEntity(ThirdSortBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ThirdSortBean readEntity(Cursor cursor, int offset) {
        ThirdSortBean entity = new ThirdSortBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getLong(offset + 1), // first_sort_id
            cursor.getLong(offset + 2), // second_sort_id
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // third_sort_id
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // third_sort_name
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // third_sort_img
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6) // frequency
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ThirdSortBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setFirst_sort_id(cursor.getLong(offset + 1));
        entity.setSecond_sort_id(cursor.getLong(offset + 2));
        entity.setThird_sort_id(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setThird_sort_name(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setThird_sort_img(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setFrequency(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ThirdSortBean entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ThirdSortBean entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ThirdSortBean entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getSecondSortBeanDao().getAllColumns());
            builder.append(" FROM THIRD_SORT_BEAN T");
            builder.append(" LEFT JOIN SECOND_SORT_BEAN T0 ON T.\"SECOND_SORT_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ThirdSortBean loadCurrentDeep(Cursor cursor, boolean lock) {
        ThirdSortBean entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        SecondSortBean secondSortBean = loadCurrentOther(daoSession.getSecondSortBeanDao(), cursor, offset);
         if(secondSortBean != null) {
            entity.setSecondSortBean(secondSortBean);
        }

        return entity;    
    }

    public ThirdSortBean loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ThirdSortBean> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ThirdSortBean> list = new ArrayList<ThirdSortBean>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ThirdSortBean> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ThirdSortBean> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
